﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using System.Collections.Generic;
using System.Data;

namespace JM.JCY.SharePointComponent.UserManagement.MemberSortOrder
{
    public partial class MemberSortOrderUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    this.ddlDepartment.DataSource = proxy.GetAllDepartment();
                    ddlDepartment.DataTextField = "DeptName";
                    ddlDepartment.DataValueField = "ID";
                    ddlDepartment.DataBind();
                    ListItem lt = new ListItem();
                    lt.Text = "请选择一个部门..";
                    lt.Value = "0";
                    ddlDepartment.Items.Insert(0, lt);
                }
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("DeptMgr.aspx");
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            DataBind();
            
        }

        private void DataBind()
        {
            int deptId = Int32.Parse(ddlDepartment.SelectedValue);
            DepartmentTitleDataSet.T_UserDepartmentDataTable dt = new DepartmentTitleDataSet.T_UserDepartmentDataTable();
            dt = GetDepartmentMemberByDeptId(deptId);
            Session["DepartmentMember"] = dt;

            DataTable tempDT = new DataTable();
            tempDT.Columns.Add("ID");
            tempDT.Columns.Add("UserName");
            tempDT.Columns.Add("UserTitle");
            UserManagementProxy proxy = new UserManagementProxy();

            foreach (var item in dt)
            {
                DataRow dr = tempDT.NewRow();
                dr["ID"] = item.ID;
                dr["UserName"] = proxy.GetUser(item.UserId, string.Empty)[0].UserName;
                tempDT.Rows.Add(dr);
            }


            RpList.DataSource = tempDT;
            RpList.DataBind();
        }


        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);


        //    if (SubmitSortPkid != null && SubmitSortPkid.Count > 0)
        //    {
        //        SaveSubmitOrder();
        //        _dataTable = GetAllDepartment();
        //    }


        //    DataTable dt = mDataTable;
        //    RpList.DataSource = dt;
        //    RpList.DataBind();
        //}




        public void UpdateDepartmentSortOrder(DepartmentTitleDataSet.T_UserDepartmentDataTable dt)
        {

            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                proxy.UpdateDepartmentMemeberSortOrder(dt);
            }

        }

        protected void SaveSubmitOrder()
        {
            DepartmentTitleDataSet.T_UserDepartmentDataTable mDataTable = new DepartmentTitleDataSet.T_UserDepartmentDataTable();
            mDataTable = (DepartmentTitleDataSet.T_UserDepartmentDataTable)Session["DepartmentMember"];

            if (SubmitSortPkid == null)
                return;
            if (SubmitSortPkid.Count <= 0)
                return;
            if (mDataTable == null)
                return;
            if (mDataTable.Rows.Count <= 0)
                return;


            mDataTable.PrimaryKey = new[] { mDataTable.Columns[0] }; //手动设置主键,与数据库中的主键设置与否无关
            int sortIndex = 1;

            foreach (int pkid in SubmitSortPkid)
            {
                DataRow dataRow = mDataTable.Rows.Find(pkid);
                if (dataRow != null)
                {
                    dataRow["SortOrder"] = sortIndex++;
                }
            }

            if (sortIndex > 1)
                UpdateDepartmentSortOrder(mDataTable);


        }


        protected ICollection<int> SubmitSortPkid
        {
            get
            {
                List<int> sortPkid = new List<int>();
                string s = Request["sortId"];
                if (!string.IsNullOrEmpty(s))
                {
                    string[] ss = s.Split(',', '|');
                    foreach (string s1 in ss)
                    {
                        int i = 0;
                        if (int.TryParse(s1, out i))
                        {
                            if (i > 0)
                                sortPkid.Add(i);
                        }
                    }
                }
                return sortPkid;
            }
        }

        //private DataSetUserManagement.T_DepartmentDataTable _dataTable;
        //protected DepartmentTitleDataSet.T_UserDepartmentDataTable mDataTable
        //{
        //    get
        //    {
        //        if (_dataTable == null)
        //            //_dataTable = GetAllDepartment();
        //        return _dataTable;
        //    }
        //}

      


        public DepartmentTitleDataSet.T_UserDepartmentDataTable GetDepartmentMemberByDeptId(int deptId)
        {
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                return  proxy.GetUserDepartmentByDeptId(deptId);
            }
          
        }

        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            SaveSubmitOrder();
            DataBind();
        }
    }
}
